package com.noahplat.device.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.noahplat.common.utils.NoahEnum;
import com.noahplat.common.utils.RedisUtils;
import com.noahplat.common.utils.Result;
import com.noahplat.common.utils.StatusCode;
import com.noahplat.device.mapper.DeviceMapper;
import com.noahplat.device.mapper.ModelMapper;
import com.noahplat.device.pojo.Device;
import com.noahplat.device.pojo.Model;
import com.noahplat.device.service.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: ModelServiceImpl
 * @Description: 设备型号实现类
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
@Service
public class ModelServiceImpl implements ModelService {

    @Autowired
    private ModelMapper modelMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private RedisUtils redisUtils;


    /**
     * 获取设备所有型号
     * @return
     */
    @Override
    public Result getModel() {
        /*先redis，在数据库*/
        List<Model> list = new ArrayList<>();
        if(redisUtils.hasKey(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode())){//从redis中获取数据
            list = (List<Model>)(List)redisUtils.lGet(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(), 0, -1);
        }else {
            list = modelMapper.selectAll();
            for (Model model : list) {
                redisUtils.lSet(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode(), model);
            }
        }
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * 分页获取设备所有型号
     * @return
     */
    @Override
    public Result selectModel(Integer page,Integer limit) {
        PageHelper.startPage(page,limit);
        List<Model> list = modelMapper.selectAll();
        if(ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"暂无数据");
        }
        PageInfo<Model> pageInfo = new PageInfo<>(list);
        return new Result(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 通过ID查询设备型号
     * @param id
     * @return
     */
    @Override
    public Result getById(Long id){
        List<Model> list = new ArrayList<>();
        if(redisUtils.hasKey(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode())){//从redis中获取数据
            list = (List<Model>)(List)redisUtils.lGet(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(), 0, -1);
        }else {
            list = modelMapper.selectAll();
            for (Model model : list) {
                redisUtils.lSet(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode(), model);
            }
        }
        for(Model model : list){
            if(model.getId() == id){
                return new Result(true,StatusCode.OK,"查询成功",model);
            }
        }
        return new Result(false,StatusCode.ERROR,"查询失败");
    }

    /**
     * 添加型号
     * @param model
     * @return
     */
    @Transactional
    @Override
    public Result addModel(Model model){
        model.setCreateTime(new Date());
        int res = modelMapper.insert(model);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"添加失败");
        }
        boolean inres = redisUtils.lSet(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode(), model);
        if(!inres){
            return new Result(false,StatusCode.ERROR,"内存存放报错");
        }
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /**
     * 修改设备型号
     * @param model
     * @return
     */
    @Transactional
    @Override
    public Result updateModel(Model model){
        int res = modelMapper.updateByPrimaryKeySelective(model);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"修改失败");
        }
        if(redisUtils.hasKey(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode())){//从redis中获取数据
            List<Object> list = redisUtils.lGet(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(), 0, -1);
            for(Object object: list){
                Model model1 = (Model) object;
                if(model1.getId() == model.getId()){
                    long delres = redisUtils.lRemove(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(),1,object);
                    if(delres == 0){
                        return new Result(false,StatusCode.ERROR,"删除内存失败");
                    }
                    boolean inres = redisUtils.lSet(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(),model);
                    if(!inres){
                        return new Result(false,StatusCode.ERROR,"插入内存失败");
                    }
                    break;
                }
            }
        }else {
           List<Model> list = modelMapper.selectAll();
            for (Model model1 : list) {
                if(model.getId() == model1.getId()){
                    redisUtils.lSet(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode(), model);
                    continue;
                }
                redisUtils.lSet(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode(), model1);
            }
        }
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /**
     * 删除设备型号
     * @param id
     * @return
     */
    @Transactional
    @Override
    public Result delModel(Long id){
        Example example = new Example(Model.class);
        example.createCriteria().andEqualTo("modelid",id);
        List<Device> list = deviceMapper.selectByExample(example);
        if(!ObjectUtils.isEmpty(list)){
            return new Result(false,StatusCode.ERROR,"禁止删除该字段,正在使用",list);
        }
        int res = modelMapper.deleteByPrimaryKey(id);
        if(res == 0){
            return new Result(false,StatusCode.ERROR,"删除失败");
        }
        if(redisUtils.hasKey(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode())){//从redis中获取数据
            List<Object> list1 = redisUtils.lGet(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(), 0, -1);
            for(Object object: list1){
                Model model1 = (Model) object;
                if(model1.getId() == id){
                   long delres =  redisUtils.lRemove(NoahEnum.MODELDIR.getCode() + NoahEnum.MODELKEY.getCode(),1,object);
                   if(delres == 0){
                       return new Result(false,StatusCode.ERROR,"删除内存出错");
                   }
                   break;
                }
            }
        }else {
            List<Model> list1 = modelMapper.selectAll();
            for (Model model1 : list1) {
                if(model1.getId() == id){
                    continue;
                }
                redisUtils.lSet(NoahEnum.MODELDIR.getCode()+NoahEnum.MODELKEY.getCode(), model1);
            }
        }
        return new Result(true,StatusCode.OK,"删除成功");
    }
}
